$(function() {
	if (isOnePage()) {
		$("#Pagination").hide();
	} else {
		$("#Pagination").show();
		$("#Pagination").pagination($("#Pagination").attr("recordCount"), {
			num_edge_entries : 2,  //两侧首尾分页条目数 
			num_display_entries : 3,  //连续分页主体部分分页条目数 
			callback : pageselectCallback,
			items_per_page : $("#Pagination").attr("pageSize")   //显示条数 
		});
	}
	$.fn.refreshPagination = function(page_index) {
		if (isOnePage()) {
			$("#Pagination").hide();
		} else {
			$("#Pagination").show();
			$("#Pagination").empty().pagination(
					$("#Pagination").attr("recordCount"), {
						num_edge_entries : 2,
						num_display_entries : 3,
						callback : pageselectCallback,
						current_page : page_index - 1,   //当前页索引  
						items_per_page : $("#Pagination").attr("pageSize")
					});
		}
	};
	// 分页
	function pageselectCallback(page_index, jq) {
		initData(page_index + 1);
	}
	;
	function isOnePage() {
		var total = $("#Pagination").attr("recordCount");
		var rp = $("#Pagination").attr("pageSize");
		if (isNaN(total)) {
			return true;
		}
		if (isNaN(rp)) {
			return true;
		}
		total = parseInt(total);
		rp = parseInt(rp);
		return total <=rp;
    }
});

function initData(page_index) {
$.ajax({
		url : $("#Pagination").attr("url"),
		type : "post",
		cache : false,
		data : {
			currentPage : page_index,
			pageSize : $("#Pagination").attr("pageSize")
		},
		success : function(data) {
			var json=eval("("+data+")");
			var showId=$("#Pagination").attr("contentDivId");
			$("#list>table>tbody").add("#post>table>tbody").add("#art>table>tbody").empty();
			if(showId=="#list"){
				var str='';
				for(var i=0;i<json.circle.length;i++){
					var intr=json.circle[i].introduction;
					if(intr.length>30){
						intr=intr.substring(0,28)+"...";
					}
					str+='<tr><td><div class="logo"><a href="circleinfo?id='+json.circle[i].id+'" target="_blank">'
					    +'<img src="../'+json.circle[i].imgpath+'" alt="'+json.circle[i].name+'" title="'+json.circle[i].introduction+'" height="48" width="48">'
					    +'</a></div></td>'
						+'<td>'+intr+'</td><td style="text-align: center;">'+json.circle[i].foundername+'</td>'
						+'<td style="text-align: center;"><a href="circleinfo?id='+json.circle[i].id+'" target="_blank">查看</a> ';
					if(json.circle[i].role!=0){
						str+=' | <a href="toEditCircle?circleid='+json.circle[i].id+'" target="_blank">管理</a>';
					}
					str+='</td></tr>';
				}
				if(str==''){
					str='<tr><td colspan="4">你还没有加入任何群组...</td></tr>'
				}
				$("#list>table>tbody").append(str);
			}
			if(showId=="#post"){
				var str="";
				for(var i=0;i<json.userPost.length;i++){
					var title=json.userPost[i].title;
					if(title.length>30){
						title=title.substring(0,28)+"...";
					}
					str+='<tr><td style="text-align: left;"><a href="post?id='+json.userPost[i].id+'" target="_blank">'+title
						+'</a></td><td><a href="circleinfo?id='+json.userPost[i].circleid+'" target="_blank">'+json.userPost[i].circlename
					    +'</a></td><td>'+(json.userPost[i].createdate).substr(0,16)+'</td></tr>';
				}
				if(str==''){
					str='<tr><td colspan="3">你还没有发表任何帖子...</td></tr>'
				}
				$("#post>table>tbody").append(str);
			}
			if(showId=="#art"){
				var str="";
				for(var i=0;i<json.userArt.length;i++){
					var title=json.userArt[i].title;
					if(title.length>30){
						title=title.substring(0,28)+"...";
					}
					str+='<tr><td style="text-align: left;"><a href="seearticle?id='+json.userArt[i].id+'" target="_blank">'+title
						+'</a></td><td><a href="circleinfo?id='+json.userArt[i].circle.id+'" target="_blank">'+json.userArt[i].circle.name
					    +'</a></td><td>'+(json.userArt[i].createdate).substr(0,16)+'</td></tr>';
				}
				if(str==''){
					str='<tr><td colspan="3">你在群组中还没有发表任何文章...</td></tr>'
				}
				$("#art>table>tbody").append(str);
			}
			
			
			$.fn.refreshPagination(page_index);
		},
		error : function(xhr) {
			alert(xhr.statusText);
		}
	});
}

function showStatus(obj){
	if(obj==1){
		return '有效';
	}else if(obj==0){
		return '无效';
	}
}

function del(obj){
	if(confirm("确认要删除吗?")){
		$.post("delNotice", { noticeId: obj.type},function(data){
			initData(1);
		});
	}
}

function modify(obj){
	var url='modNotice?id='+obj.type;
	obj.href=url;
}
